fix: Handle ProcessTransport race condition in control request handlers #336
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #266
Problem
When using SDK MCP servers (
create_sdk_mcp_server()), queries crash with:This error occurs in
_internal/query.pyat lines 303 and 315 when control request handlers try to write responses.Root Cause
Control requests are handled in separate background tasks via
_tg.start_soon(self._handle_control_request, request)(line 183).During query cleanup, the transport is marked as not ready (
self._ready = False) and streams are closed. However, pending control request handler tasks may still be running and attempt to write their responses, causing the crash.Race condition timeline:
Solution
Wrap the
transport.write()calls in try-except blocks to gracefully handle when the transport is not ready. This allows control request handlers to fail silently if they run during cleanup, rather than crashing the entire query.Changes
File:
src/claude_agent_sdk/_internal/query.pyChange 1: Success response write (line 303)
Before:
After:
Change 2: Error response write (line 315)
Before:
After:
Testing
Tested with SDK MCP servers using the scenario from issue #266:
Before fix: Consistent crash with
ProcessTransport is not ready for writingAfter fix: Clean execution, graceful cleanup
Impact
Additional Context
This fix was discovered and tested while building a production application using Claude Agent SDK with custom MCP tools. The race condition was consistently reproducible and this fix eliminates the crash entirely.
The issue reporter in #266 did extensive debugging work to identify this bug. This PR provides the fix they were looking for.